package com.frxuexi.park2304.notice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityNoticeListBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NoticeListActivity")
@AndroidEntryPoint
class NoticeListActivity : BaseActivity<ActivityNoticeListBinding,NoticeIntent,NoticeViewModel>() {
    override val binding: ActivityNoticeListBinding by lazy { ActivityNoticeListBinding.inflate(layoutInflater) }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()
    val noticeAdapter2 by lazy { NoticeAdapter2() }
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val items = it.data as Collection<NoticeBeanItem>
                        val list = items.filter { it.title.isNotEmpty() }
                        for (i in 0..list.size-1){
                            list.get(i).myType=i%2
                        }
                        noticeAdapter2.addData(list)
                        noticeAdapter2.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.headAdd.setOnClickListener{
            ArouterManager.nav(this,"/app/NoticeActivity")
        }
        binding.headBack.setOnClickListener {
            finish()
        }
        binding.noticeRv.apply {
            adapter=noticeAdapter2
            layoutManager=LinearLayoutManager(this@NoticeListActivity)
        }

        noticeAdapter2.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/app/NoticeDetailActivity",noticeAdapter2.data.get(position).id!!)
        }

    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(NoticeIntent.getNoticeList(1))
    }

}